import multiprocessing
import time


def wait_for_event(e):
    e.wait()
    print(f"{multiprocessing.current_process().name}睡眠1秒")
    time.sleep(1)
    e.clear()
    print(f"{time.strftime('%H:%M:%S')}进程A:我们是好兄弟，我等你。。。")
    e.wait()
    print(f"{time.strftime('%H:%M:%S')}进程A:好的，是兄弟一起走")


def wait_for_event_timeout(e, t):
    e.wait()
    print(f"{multiprocessing.current_process().name}睡眠1秒")
    time.sleep(1)
    e.clear()
    print(f"{time.strftime('%H:%M:%S')}进程B:最多等你{t}秒")
    e.wait(t)
    print(f"{time.strftime('%H:%M:%S')}进程B:先走一步")


if __name__ == "__main__":
    e = multiprocessing.Event()
    w1 = multiprocessing.Process(target=wait_for_event, args=(e,))
    w2 = multiprocessing.Process(target=wait_for_event_timeout, args=(e, 9))
    w1.start()
    w2.start()
    print(f"{time.strftime('%H:%M:%S')} 主进程：谁等一下我，我需要8秒时间")
    e.set()
    time.sleep(8)
    print(f"{time.strftime('%H:%M:%S')} 主进程：好了，我赶上了")
    e.set()
    w1.join()
    w2.join()
    print(f"{time.strftime('%H:%M:%S')} 主进程：退出")

